package ${groupId}.service.system.resource;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ${groupId}.dal.mybatis.mapper.SysResourceMapper;
import ${groupId}.dal.mybatis.model.SysResource;
import ${groupId}.dal.mybatis.model.SysResourceExample;
import ${groupId}.service.system.resource.dto.SysResourceAddItemReqDto;
import ${groupId}.service.system.resource.dto.SysResourceAddItemRspDto;
import ${groupId}.service.system.resource.dto.SysResourceTree;
import ${groupId}.service.system.resource.dto.SysResourceUpdateItemReqDto;
import com.system.core.results.ResultBase;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SysResourceService {
	
	@Autowired
	private SysResourceMapper sysResourceMapper;
	
	public List<SysResourceTree> getMenusByParentId(long menuid){
		List<SysResourceTree> rootnodes = new ArrayList<SysResourceTree>();
		SysResourceExample example = new SysResourceExample();
		example.createCriteria().andParentResourceIdEqualTo(menuid);
		List<SysResource> sysResources = sysResourceMapper.selectByExample(example);
		List<SysResource> allMenu = sysResourceMapper.selectByExample(null);
		for(SysResource sysResource:sysResources){
			SysResourceTree rootnode = new SysResourceTree();
			rootnode.setHref(sysResource.getUrl());
			rootnode.setId(sysResource.getId().intValue());
			rootnode.setSpread(sysResource.getSpread());
			rootnode.setTitle(sysResource.getName());
			
			rootnode.setName(sysResource.getName());
			rootnode.setType(sysResource.getType());
			rootnode.setPermission(sysResource.getPermission());
			rootnode.setUrl(sysResource.getUrl());
			rootnode.setCreateTime(sysResource.getCreateTime());
			rootnode.setUpdateTime(sysResource.getUpdateTime());
			rootnodes.add(nodebuild(rootnode,allMenu));
		}
		
		return rootnodes;
	}
	
	private SysResourceTree nodebuild(SysResourceTree pnode,List<SysResource> list){
		for(SysResource nodedo:list){
			if(nodedo.getParentResourceId()!=0 && nodedo.getParentResourceId() == pnode.getId()){
				SysResourceTree node = new SysResourceTree();
				node.setHref(nodedo.getUrl());
				node.setId(nodedo.getId().intValue());
				node.setSpread(nodedo.getSpread());
				node.setTitle(nodedo.getName());
				
				node.setName(nodedo.getName());
				node.setType(nodedo.getType());
				node.setPermission(nodedo.getPermission());
				node.setUrl(nodedo.getUrl());
				node.setCreateTime(nodedo.getCreateTime());
				node.setUpdateTime(nodedo.getUpdateTime());
				pnode.getChildren().add(node);
				nodebuild(node,list);
			}
		}
		return pnode;
	}
	
	/**
	 * 根据主键获取单条数据
	 * @param sysResource 使用sysResource对象的主键属性Id类型是Long
	 */
	public SysResource sysResourceGetByPrimaryKey(SysResource sysResource) {
		SysResource result = sysResourceMapper.selectByPrimaryKey(sysResource.getId());
		return result;
	}
	
	/**
	 * 根据主键更新数据
	 * @param sysResource 被更新对象（请注意，只更新有值属性）
	 */
	public ResultBase sysResourceUpdateByPrimaryKey(SysResourceUpdateItemReqDto sysResourceUpdateItemReqDto) {
		ResultBase resultBase = new ResultBase();
		SysResource sysResource = new SysResource();
		BeanUtils.copyProperties(sysResourceUpdateItemReqDto, sysResource);
		sysResourceMapper.updateByPrimaryKeySelective(sysResource);
		resultBase.setSuccess(true);
		resultBase.setMessage("更新成功");
		return resultBase;
	}
	
	/**
	 * 根据主键删除数据
	 * @param keys 被删除对象主键List集合
	 */
	@Transactional
	public ResultBase sysResourceDeleteByPrimaryKey(List<Long> keys) {
		ResultBase resultBase = new ResultBase();
		List<SysResource> all = sysResourceMapper.selectByExample(null);
		try {
			keys.forEach((key)->{
				sysResourceMapper.deleteByPrimaryKey(key);
				deleteAllChild(key,all);
			});
		} catch (Exception e) {
			resultBase.setMessage(e.getMessage());
			return resultBase;
		}
		resultBase.setSuccess(true);
		resultBase.setMessage("成功删除"+keys.size()+"条记录");
		return resultBase;
	}
	
	public void deleteAllChild(Long key,List<SysResource> all) {
		for(SysResource resource:all) {
			if(resource.getParentResourceId() == key) {
				sysResourceMapper.deleteByPrimaryKey(resource.getId());
				deleteAllChild(resource.getId(),all);
			}
		}
	}
	
	/**
	 * 新增数据
	 * @param sysResourceAddItemReqDto 新增对象
	 */
	@Transactional
	public SysResourceAddItemRspDto sysResourceSave(SysResourceAddItemReqDto sysResourceAddItemReqDto) {
		SysResourceAddItemRspDto resultBase = new SysResourceAddItemRspDto();
		SysResource sysResource = new SysResource();
		BeanUtils.copyProperties(sysResourceAddItemReqDto, sysResource);
		sysResourceMapper.insertSelective(sysResource);
		resultBase.setData(sysResource);
		resultBase.setSuccess(true);
		resultBase.setMessage("新增成功");
		return resultBase;
	}
}
